﻿using ProjectScheduling.daos;
using ProjectScheduling.models;

namespace ProjectScheduling
{
    public partial class EditPassForm : Form
    {
        public static readonly EditPassForm b = new();
        private readonly Users userMsg = Program.LoginUser ?? new Users();
        public Action? action;
        public EditPassForm()
        {
            InitializeComponent();
            if (Program.LoginUser == null)
            {
                Application.Exit();
            }
            txt_name.Text = userMsg.Name;

        }

        private void Btn_login_Click(object sender, EventArgs e)
        {
            var pass = txt_oldpass.Text?.Trim() ?? "";
            var newpass = txt_newpass.Text?.Trim() ?? "";
            var repeatpass = txt_repeatpass.Text?.Trim() ?? "";
            if (string.IsNullOrEmpty(pass))
            {
                MessageBox.Show("请输入当前密码");
                return;
            }
            if (string.IsNullOrEmpty(newpass))
            {
                MessageBox.Show("请输入新密码");
                return;
            }
            if (newpass != repeatpass)
            {
                MessageBox.Show("两次新密码输入不一致");
                return;
            }
            pass = Common.Md5(pass);
            if (userMsg.Password != pass)
            {
                MessageBox.Show("当前密码不正确");
                return;
            }
            userMsg.Password = Common.Md5(newpass);
            UsersDao.Base.Update(userMsg, it => new { it.Password });
            MessageBox.Show("修改密码成功");
            action?.Invoke();
            Close();
        }
    }
}
